package com.bitdf.txing.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bitdf.txing.product.dao.BrandDao;
import com.bitdf.txing.product.dao.CategoryBrandRelationDao;
import com.bitdf.txing.product.entity.BrandEntity;
import com.bitdf.txing.product.entity.CategoryBrandRelationEntity;
import com.bitdf.txing.product.feign.ProdcutWareFeignService;
import com.bitdf.txing.product.service.BrandService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {


    @Autowired
    CategoryBrandRelationDao categoryBrandRelationDao;

//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//        String key=(String)params.get("key");
//        QueryWrapper<BrandEntity> queryWrapper = new QueryWrapper<>();
//        if (!StringUtils.isEmpty(key))
//        {
//            queryWrapper.eq("brand_id", key).or().like("name", key);
//        }
//        IPage<BrandEntity> page = this.page(
//                new Query<BrandEntity>().getPage(params),
//                queryWrapper
//        );
//
//        return new PageUtils(page);
//    }

    @Override
    public void updateDetail(BrandEntity brand) {
        this.updateById(brand);
        if(!StringUtils.isEmpty(brand.getName()))
        {
            // TODO 改了
            CategoryBrandRelationEntity relation=new CategoryBrandRelationEntity();
            relation.setBrandId(brand.getId());
            relation.setBrandName(brand.getName());
            categoryBrandRelationDao.update(relation, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_Id", brand.getId()));

            //TODO 更新其他关联
        }
    }

    @Override
    public List<BrandEntity> getBrandsByIds(List<Long> brandIds) {
        List<BrandEntity> entities = baseMapper.selectList(new QueryWrapper<BrandEntity>().in("id", brandIds));
        return entities;
    }

    @Autowired
    ProdcutWareFeignService wareFeignService;

    @Override
    @Transactional
    @GlobalTransactional
    public void testSeata() {
        BrandEntity brandEntity = new BrandEntity();
        brandEntity.setName("WS");
        this.save(brandEntity);

        wareFeignService.testSeata();

        int i = 5 / 0;
    }


}